package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import process.*;
import databaseAcces.*;

/**
 * Servlet implementation class ControllerJobMangement
 */
public class ControllerJobManagement extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ControllerJobManagement() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated constructor stub
		HttpSession session = request.getSession();
		
		// Verifier que l'utilisateur a login 
		System.out.println("Login:" + (String)session.getAttribute("login"));
		
		 if ((String)session.getAttribute("login") == null)
		 {
			 // il n'a pas login 
			 response.sendRedirect("SignIn.jsp");	
		 }
		 else
		 {
				// caracteres en unicode
				request.setCharacterEncoding("UTF-8");	
				String num_En = (String)session.getAttribute("num_En");	
				
				//parametre_Poste [0]: "titre"		   				
				//parametre_Poste [1]: "specialite"
				//parametre_Poste [2]: "domaine"
				//parametre_Poste [3]: "salaire_propose"
				//parametre_Poste [4]: "experience_requis"
				//parametre_Poste [5]: "lieu_travail"	   					
				//parametre_Poste [6]: "niveau"
				//parametre_Poste [7]: "description"
				//parametre_Poste [8]: "num_En"
				//parametre_Poste [9]: "date_limite"				
				
				Vector<String> parametre_Poste = new Vector<String>();
				parametre_Poste.add(request.getParameter("position_wanted"));
				parametre_Poste.add(request.getParameter("specialite"));
				parametre_Poste.add(request.getParameter("domain"));
				parametre_Poste.add(request.getParameter("salary_wanted"));
				parametre_Poste.add(request.getParameter("year_experience"));
				parametre_Poste.add(request.getParameter("lieu_travail"));
				parametre_Poste.add(request.getParameter("levelstudy"));
				parametre_Poste.add(request.getParameter("description_job"));
				parametre_Poste.add(num_En);
				parametre_Poste.add(request.getParameter("date_limite"));
				
				// verifier que l'entreprise a engeristre la poste
				// + Si Oui : nous allons mettrer a jours 
				// + Si Non : engeristrer une nouvelle poste
				DisplayRequest dis = new DisplayRequest();
				Vector<String> Poste = dis.getPoste_num_En(Integer.parseInt(num_En));
				
				JobManagement job = new JobManagement();
				
			
				if(Poste.size() > 0)
				{
					job.UpdateJob(Integer.parseInt(num_En), parametre_Poste);
					
				}
				else
				{
					job.saveJob(parametre_Poste);
				}
												
				 // update les session	
				session.setAttribute("position_wanted",request.getParameter("position_wanted"));
				session.setAttribute("specialite",request.getParameter("specialite"));
				session.setAttribute("domain",request.getParameter("domain"));
				session.setAttribute("salary_wanted",request.getParameter("salary_wanted"));
				session.setAttribute("year_experience",request.getParameter("year_experience"));						
				session.setAttribute("lieu_travail",request.getParameter("lieu_travail"));
				session.setAttribute("levelstudy",request.getParameter("levelstudy"));
				session.setAttribute("description_job",request.getParameter("description_job"));						
				session.setAttribute("date_limite",request.getParameter("date_limite"));
	
				
			// reload la page
				 response.sendRedirect("JobManagement.jsp");
				 
		 }
				
	}

}
